<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>商品管理</title>
	<%@ include file="/WEB-INF/pages/common/frameCommonJs.jsp"  %>
	<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/engine.js'></script>
	<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/util.js'></script>
	<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/commonService.js'></script>
	<script>
		var saveOrUpdateUrl = "<%=request.getContextPath()%>/product/saveProduct.action";
		function savePro(){  
		    $('#fm').form('submit',{  
		        url:saveOrUpdateUrl,  
		        onSubmit: function(){  
		        	var proClassName = $("#proClassName").val();
		        	if(proClassName.length == 0){
		        		$.messager.alert("提示信息","请选择商品分类!",'warning');
		        		return false;
		        	}
		        	var retailPrice = $("#pro\\.retailPrice").val();
		        	var lowestPrice = $("#pro\\.lowestPrice").val();
		        	if((new Number(retailPrice)).valueOf() < (new Number(lowestPrice)).valueOf()){
		        		$.messager.alert("提示信息","最低售价不能大于零售价!",'warning');
		        		return false;
		        	}
		        	// 当前库存
		        	var initStock = $("#pro\\.initStock").val();
		        	var warehouse = $("#pro\\.initWareHouse\\.id").val();
		        	if(initStock > 0){
		        		if(warehouse == 0){
		        			$.messager.alert("提示信息","请选择仓库!",'warning');
		        			return false;
		        		}
		        	}
		        	if(warehouse != 0){
		        		if($.trim(initStock).length == 0){
		        			$.messager.alert("提示信息","初始库存数不能为空!",'warning');
		        			return false;
		        		}
		        	}
		            return $(this).form('validate');  
		        },  
		        success: function(result){  
		            var result = eval('('+result+')');
		            var msg = result.name;
		            if($("#pro\\.id").val()!=""){
		            	msg = msg + "修改成功";
		            }else{
		            	msg = msg + "新增成功";
		            }
		            if (result!=''){
		            	$.messager.show({title:"提示信息:",msg:msg,showType:'slide'});
		                $('#dlg').dialog('close');      // close the dialog  
		                $('#products').datagrid('reload');    // reload the user data  
		            } else {  
		                $.messager.show({  
		                    title: '新增成功',  
		                    msg: result.msg  
		                });  
		            }  
		        }  
		    });  
		}
		
		function exportExcel(){ 
			saveOrUpdateUrl = "<%=request.getContextPath()%>/product/exportExcelProducts.action";
		    $('#searchForm').form('submit',{  
		        url:saveOrUpdateUrl,  
		        onSubmit: function(){  
		        	var proName = $("#proName").val();
		        	var pcId = $("#pcId").val();
		            return $(this).form('validate');  
		        },  
		        success: function(result){  
		            var result = eval('('+result+')');
		            if (result==true){
		            	$.messager.show({title:"提示信息:", msg:"导出成功！"});
		            } else {  
		                $.messager.show({title:"提示信息:", msg:"导出失败！"});  
		            }  
		        }  
		    });  
		}
		
		function toEditPro(){
			$("#tr1").show();
			saveOrUpdateUrl = "<%=request.getContextPath()%>/product/updateProduct.action";
			var node=$("#products").datagrid("getSelected");
			if(node == null){
				$.messager.alert("提示信息:","请选择商品!");
				return;
			}
			$('#dlg').dialog('open').dialog('setTitle','修改商品');
			$('#dlg').form('clear');
			$("#pro\\.id").val(node.id);
			$("#pro\\.name").val(node.name);
			$("#pro\\.proNo").val(node.proNo);
			$("#pro\\.unit").val(node.unit);
			$("#pro\\.spec").val(node.spec);
			$("#pro\\.note").val(node.note);
			$("#pro\\.retailPrice").val(node.retailPrice.toFixed(2));
			$("#pro\\.lowestPrice").val(node.lowestPrice.toFixed(2));
			$("#proClassName").val(node.attributes.proClassName);
			$("#pro\\.proClass\\.id").val(node.attributes.proClassId);
			$("#pro\\.initStock").val(node.initStock);
			$("#pro\\.initWareHouse\\.id").val(node.wareHouseId);
			$("#pro\\.initWareHouse\\.name").val(node.wareHouseName);
		}
		
		function toCopyPro(){
			$("#tr1").show();
			saveOrUpdateUrl = "<%=request.getContextPath()%>/product/saveProduct.action";
			var node=$("#products").datagrid("getSelected");
			if(node == null){
				$.messager.alert("提示信息:","请选择商品!");
				return;
			}
			$('#dlg').dialog('open').dialog('setTitle','复制商品');
			$('#dlg').form('clear');
			//$("#pro\\.id").val(node.id);
			$("#pro\\.name").val(node.name);
			$("#pro\\.proNo").val(node.proNo);
			$("#pro\\.unit").val(node.unit);
			$("#pro\\.spec").val(node.spec);
			$("#pro\\.note").val(node.note);
			$("#pro\\.retailPrice").val(node.retailPrice.toFixed(2));
			$("#pro\\.lowestPrice").val(node.lowestPrice.toFixed(2));
			$("#proClassName").val(node.attributes.proClassName);
			$("#pro\\.proClass\\.id").val(node.attributes.proClassId);
			$("#pro\\.initStock").val(node.initStock);
			$("#pro\\.initWareHouse\\.id").val(node.wareHouseId);
			$("#pro\\.initWareHouse\\.name").val(node.wareHouseName);
		}
		
		$(function(){
				$('.icon-reload').click(
					function (){ 
						$('#pcTree').tree({url:'<%=request.getContextPath()%>/product/getAllPoductClassJson.action'});
						$('#pcTree').tree('reload')
					}
				);
				$(".icon-reload").css("cursor","pointer");
				$('#pcTree').tree({
					animate: true,
					url: '<%=request.getContextPath()%>/product/getAllPoductClassJson.action',
					onBeforeExpand:function(node){
						$('#pcTree').tree("options").url=getClassUrl + "?pc.id=" + node.id;
					},
					onClick:function(node){
						var node = $('#pcTree').tree('getSelected');
						$('#products').datagrid('reload',{pcId:node.id});
						$('#pcTree').tree('expand',node.target);
						$('#products').datagrid({pageNumber:1});// 将表格默认设为1
						$('#pcId').val(node.id);//点击商品类别的时候，将当前类别Id设置到查询表单中
					},
					onContextMenu: function(e, node){
						e.preventDefault();
						$('#pcTree').tree('select', node.target);
						$('#mm').menu('show', {
							left: e.pageX,
							top: e.pageY
						});
					}
				});
			   var getClassUrl = "<%=request.getContextPath()%>/product/getAllPoductClassJson.action";
			   // 商品表格
			   $('#products').datagrid({
			   	fit:true,
				nowrap: false,
				striped: true,
				url:'<%=request.getContextPath()%>/product/getAllProducts.action',
				sortName: 'code',
				sortOrder: 'desc',
				remoteSort: false,
				idField:'code',
				singleSelect:true,
				title:'商品列表',
				columns:[[
					{field:'proNo',title:'编号',width:120},
					{field:'name',title:'名称',width:120},
					{field:'proClassName',title:'商品分类',width:120},
					/* {field:'unit',title:'单位',width:120,rowspan:2}, */
					{field:'spec',title:'规格',width:150,rowspan:2},
					{field:'retailPrice',title:'零售价(元)',width:150,rowspan:2,align:'right',
						formatter:function(value,row,index){
							return row.retailPrice.toFixed(2);
						},sortable:true
					},
					{field:'lowestPrice',title:'成本价(元)',width:150,rowspan:2,align:'right',
						formatter:function(value,row,index){
							return row.lowestPrice.toFixed(2);
						},sortable:true
					},
				]],
				pageSize:15,  
                pageList:[5,10,15,20],  
                fitColumns: true,  
				pagination:true,
				rownumbers:true
			});
			   
		   $('a[name=query]').click(function(){
				var proName = $('#proName').val();
				$('#products').datagrid('reload',{proName:proName});
				$('#products').datagrid({pageNumber:1});
			});
		
			$('#proName').keydown(function(e){
		      if(e.keyCode==13) {
		    	  var proName = $('#proName').val();
		    	  $('#products').datagrid('reload',{proName:proName});
		      }
		    });
		}); 
		function toAddPro(){
	        $("#tr1").hide();
			saveOrUpdateUrl = "<%=request.getContextPath()%>/product/saveProduct.action";
			$('#dlg').dialog('open').dialog('setTitle','新增商品');
			$('#dlg').form('clear');
			
		}
		function toDelPro(){
			var node = $('#products').datagrid('getSelected');
			var condt = new delOrgProWhCondtn();
			condt.pid = node.id;
			commonService.valDelOrgProWH(condt,function(data){
				if(data=="no"){
					$.messager.alert("提示信息","该商品已在业务单据中使用，不能删除!",'warning');
					return;
				}
				$.messager.confirm('删除商品信息', '确认删除[' + node.name + ']吗?', function(r){
					if(r){
						$.ajax({
							type:"post",
							data:"pro.id=" + node.id,
							url:"<%=request.getContextPath()%>/product/deleteProById.action",
							success:function(result){
								var data = eval('(' + result + ')');
								$.messager.show({title:"提示信息",msg: node.name + "删除成功",showType:'slide'});
								$('#products').datagrid('reload');
							}
						});
						
					}
				});
			});
		}
		function clearData(){
			$('#searchForm').form('clear');
		}
		
		function openWareHouse(){
	   		var title = "所属仓库选择";
	   		$('#wareHouseDlg').dialog('open').dialog('setTitle',title);  
   		}
   		function selectWareHouse(){
	   		var node = $('#wars').datagrid("getSelected");
	   		$("#pro\\.initWareHouse\\.name").val(node.name);
	   		$("#pro\\.initWareHouse\\.id").val(node.id);
	   		$('#wareHouseDlg').dialog('close');
   		}
   		function removeInitStock() {
   			$("#pro\\.initStock").val("");
   			$("#pro\\.initWareHouse\\.id").val("");
   			$("#pro\\.initWareHouse\\.name").val("");
   		}
	</script>
</head> 
<body class="easyui-layout" >
	<!-- 
	<h2>商品列表</h2>
	<div class="demo-info" style="margin-bottom:10px">
		<div class="demo-tip icon-tip"></div>
		<div>Click the row to start editing.</div>
	</div>
	 -->
	 
	<div region="west" iconCls="icon-reload" title="商品类别" split="true" style="width:180px;">
		<ul id="pcTree"></ul>
	</div>
	<div region="center" title="商品信息管理" style="overflow:auto;">
		<div class="easyui-layout" data-options="fit:true">
			<div style="height:100px" data-options="region:'north',border:false">
				<div id="tb" class="datagrid-toolbar">  
				    <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="javascript:toAddPro()">新增</a>  
				    <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="javascript:toEditPro();">修改</a>  
				    <a href="#" class="easyui-linkbutton" iconCls="icon-copy" plain="true" onclick="javascript:toCopyPro()">复制</a>
				    <a href="#" class="easyui-linkbutton" iconCls="icon-excel" plain="true" onclick="javascript:exportExcel()">导出</a>
				    <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="javascript:toDelPro()">删除</a>
				</div>
				<form id="searchForm" method="post">
				<table style="font-size:12px; background:#fafafa; padding:10px;" border="0">
					<tr>
						<td>&nbsp;商品名称
							<input type="text" id="proName" name="proName" class="9"  value="${proName}" />
							<input type="hidden" id="pcId" name="pcId" value="" />
						</td>
						<td>
							<a href="#" class="easyui-linkbutton" type="submit" name="query">查询</a>
			 				<a href="#" class="easyui-linkbutton" onclick="clearData()">重置</a>
						</td>
					</tr>
					<tr style="display:none">
						<td>&nbsp;<input /></td>
					</tr>
				</table>
				</form>
			</div>
			<div data-options="region:'center',border:false">
				<table id="products"></table>
			</div>
		</div>
	</div>
	<div id="dlg" class="easyui-dialog" style="width:440px"  
        closed="true" buttons="#pro-dlg-buttons" resizable="true">  
    <form id="fm" method="post">
    <center>
    <table border="0">
    	<input id="pro.id" name="pro.id" type="hidden" />  
    	<input id="pro.proClass.id" name="pro.proClass.id" type="hidden" />  
        	<tr>
        		 <td align="right">名称:</td>
        		 <td align="left">
        		 	<input id="pro.name" name="pro.name" class="easyui-validatebox" required="true" size="40" maxlength="50" />
        		 	<font color="red">*</font>
        		 </td>  
        	</tr>
        	<tr>
        		<td align="right" >编号:</td>
            	<td align="left">
            		<input id="pro.proNo" name="pro.proNo" class="easyui-validatebox" required="true" maxlength="20"  />
            		<font color="red">*</font>
            	</td>
            </tr>
            <tr>
        		<td align="right">零售价:</td>
            	<td align="left">
            		<input id="pro.retailPrice" name="pro.retailPrice" class="easyui-validatebox" required="true" maxlength="15" validType="currency" />
            		<font color="red">*</font>
            	</td>
            </tr>
            <tr>
        		<td align="right">成本价:</td>
            	<td align="left">
            		<input id="pro.lowestPrice" name="pro.lowestPrice" class="easyui-validatebox" required="true" maxlength="15" validType="currency" />
            		<font color="red">*</font>
            	</td>
            </tr>
            <!-- <tr>
            	<td>单位:</td>
            	<td>
            		<input id="pro.unit" name="pro.unit" class="easyui-validatebox" required="true" maxlength="20" />  
            	</td>
            </tr> -->
            <tr>
            	<td align="right">规格:</td>
            	<td align="left">
            		<input id="pro.spec" name="pro.spec" class="easyui-validatebox" required="true" maxlength="20" />
            		<font color="red">*</font>
            	</td>
            </tr>
            <tr>
            	<td align="right">
            		商品分类:
            	</td>
            	<td align="left">
            		<input type="text"  name="proClassName" id="proClassName" class="easyui-validatebox" readonly="readonly" onclick="openSelectPC()" style="PADDING-LEFT: 20px;BACKGROUND-IMAGE: url(<%=request.getContextPath()%>/images/note_add.png); BACKGROUND-REPEAT: no-repeat;cursor:pointer;" size="17" /> 
            		<font color="red">*</font>
            	</td>
            </tr>
			<tr>
				<td colspan="2">
					<hr align="center" style= "border:1 dotted #666666" /> 
				</td>
			</tr>
            <tr>
            	<td align="right">初始库存数:</td>
            	<td align="left">
            		<input id="pro.initStock" name="pro.initStock" class="easyui-validatebox" maxlength="9"  validType="integer" />
            	</td>
            </tr>
            <tr>
            	<td align="right">所属仓库:</td>
            	<td align="left">
            		<input type="hidden" id="pro.initWareHouse.id" name="pro.initWareHouse.id" onclick="openWareHouse()" value="${pro.initWareHouse.id}" />
            		<input type="text" id="pro.initWareHouse.name" name="pro.initWareHouse.name" onclick="openWareHouse()" readonly="readonly" class="easyui-validatebox" value="${pro.initWareHouse.name}" style="PADDING-LEFT: 20px;BACKGROUND-IMAGE: url(<%=request.getContextPath()%>/images/note_add.png); BACKGROUND-REPEAT: no-repeat;cursor:pointer;" />
            	</td>
            </tr>
            <tr id="tr1">
            	<td>
            		&nbsp;
            	</td>
            	<td align="left">
            		<a href="#" onclick="removeInitStock()">去除当前库存</a>
            	</td>
            </tr>
            <tr>
            	<td align="right">
            		描述:
            	</td>
            	<td align="left">
           			<textarea id="pro.note" name="pro.note" class="easyui-validatebox" rows="4" cols="25" maxlength="200"></textarea>
           		</td>
           	</tr>
        </table>
        </center>
        </form> 
	</div>
	<div id="pro-dlg-buttons">  
	    <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="savePro()">保存</a>  
	    <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>  
	</div> 
<%@ include file="/WEB-INF/pages/common/productClassSelect.jsp"  %>
<%@ include file="/WEB-INF/pages/common/warehouseSelect.jsp"  %>
</body>
</html>